﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Sinacor.Infra.UI.Common;
using Sinacor.Infra.UI.Session;
using Sinacor.Infra.UI.Messaging.Internal;
using Sinacor.Infra.UI.Messaging.SmartClient;
using Sinacor.Infra.UI.Messaging;
using System.Threading;
using Sinacor.Infra.Common.Exceptions;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
    public class SinacorWindowMenu : Window, IBaseView
    {
        private SessionInformation _sessionInformation = null;
        private WPFMessageManager _wpfMessageManager;

        public SessionInformation Session
        {
            get { return _sessionInformation; }
            set { _sessionInformation = value; }
        }

        public SinacorWindowMenu()
        {
			this.ResizeMode = ResizeMode.NoResize;
            this.SetResourceReference(StyleProperty, "ControlWindowMenu");

            if (SessionManager.GetInstance().GetCurrentSession() != null)
            {
                this._sessionInformation = SessionManager.GetInstance().GetCurrentSession().GenerateSessionInformation();
            }

            _wpfMessageManager = new WPFMessageManager();
        }

        #region IBaseView Members

        /// <summary>
        /// Metodo que pega as informacoes da sessao do usuario
        /// </summary>
        /// <returns></returns>
        public SessionInformation GetSessionInformation()
        {
            return _sessionInformation;
        }

        public void ShowError(Exception exception)
        {
            ShowError(exception, ErrorLevel.Normal);
        }

        public void ShowError(IEnumerable<Exception> exceptions)
        {
            if (exceptions == null)
                throw new ArgumentNullException("exceptions");

            foreach (Exception o in exceptions)
            {
                ShowError(o);
            }
        }

        public void ShowError(Exception exception, ErrorLevel errorLevel)
        {
            if (Thread.CurrentThread == this.Dispatcher.Thread)
            {
                SinacorWindow.LogError(exception);

                SinacorMessageBox.Show(exception.Message, SinacorMessageBoxButton.Ok, SinacorMessageBoxImage.Exclamation);
            }
            else
            {
                this.Dispatcher.Invoke(new Action<Exception, ErrorLevel>(ShowError), exception, errorLevel);
            }
        }

        public void ShowError(string resourceKey, params object[] parameters)
        {
            if (Thread.CurrentThread == this.Dispatcher.Thread)
            {
                if (string.IsNullOrEmpty(resourceKey))
                    throw new ArgumentNullException("resourceKey");

                ErrorDetail errorDetail = ErrorDetail.CreateFromResourceKey(resourceKey, ErrorType.ServerError, parameters);

                SinacorMessageBox.Show(errorDetail.Message, SinacorMessageBoxButton.Ok, SinacorMessageBoxImage.Exclamation);
            }
            else
            {
                this.Dispatcher.Invoke(new Action<string, object[]>(ShowError), resourceKey, parameters);
            }
        }

        /// <summary>
        /// Implementação da renovação da sessão do usuário.
        /// </summary>
        public void RenewSession()
        {
            //TODO: Enviar mensagem para a aplicação para renovação da sessão.
            InternalMessage message = new InternalMessage();

            //Alterar para mensagem de sessão expirada.
            message.MessageId = MessageIds.Application.SNC_APPLICATION_INACTIVITY;
            _wpfMessageManager.SendMessage(message);
        }


        public void NotifyChanges()
        {
            throw new NotSupportedException();
        }

        public void ClearChanges()
        {
            throw new NotSupportedException();
        }

        #endregion
    }
}
